﻿using Microsoft.Extensions.Primitives;
using NetMicro.Core.Extensions;
using NetMicro.Core.Helper;

namespace NetMicro.Core.ApiFunction
{
    /// <summary>
    /// api请求上下文
    /// </summary>
    public static class ApiContext
    {

        /// <summary>
        /// IsAdminApi
        /// </summary>
        public static bool IsAdminApi
        {
            get
            {
                StringValues isAdminApi = "";
                WebHelper.HttpContextAccessor?.HttpContext?.Request?.Headers.TryGetValue("isAdminApi", out isAdminApi);
                return isAdminApi.ToBool();
            }
        }

        /// <summary>
        /// requestId
        /// </summary>
        public static string RequestId
        {
            get
            {
                StringValues requestId = "";
                WebHelper.HttpContextAccessor?.HttpContext?.Request?.Headers.TryGetValue("requestId", out requestId);
                return !string.IsNullOrWhiteSpace(requestId) ? (string)requestId : default;
            }
        }

        /// <summary>
        /// 管理端上下文 
        /// </summary>
        public static AdminContext AdminContext
        {
            get
            {
                StringValues adminContext = "";
                WebHelper.HttpContextAccessor?.HttpContext?.Request?.Headers.TryGetValue("adminContext", out adminContext);
                if (!string.IsNullOrWhiteSpace(adminContext))
                    return ((string)adminContext).ToObject<AdminContext>();
                return new AdminContext();
            }
        }

        /// <summary>
        /// 客户端上下文 
        /// </summary>
        public static WebContext WebContext
        {
            get
            {
                StringValues webContext = "";
                WebHelper.HttpContextAccessor?.HttpContext?.Request?.Headers.TryGetValue("webContext", out webContext);
                if (!string.IsNullOrWhiteSpace(webContext))
                    return ((string)webContext).ToObject<WebContext>();
                return new WebContext();
            }
        }

    }

   
}
